package com.assignwork.service.impl;

import com.assignwork.dto.WorkArrangeFixedMachineSubmitDTO;
import com.assignwork.dto.WorkArrangeMachinesSubmitDTO;
import com.assignwork.entity.WorkArrangeFixedMachine;
import com.assignwork.entity.WorkArrangeMachines;
import com.assignwork.mapper.WorkArrangeFixedMachineMapper;
import com.assignwork.service.IWorkArrangeFixedMachineService;
import com.assignwork.vo.WorkArrangeFixedMachineVO;
import com.assignwork.vo.WorkArrangeMachinesVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.cache.DictBizCache;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName WorkArrangeFixedMachineServiceImpl
 * @Description 配工固机接口实现
 * @Author dujianyu
 * @Date 2023/12/19 10:19
 */
@Service
public class WorkArrangeFixedMachineServiceImpl extends BaseServiceImpl<WorkArrangeFixedMachineMapper, WorkArrangeFixedMachine> implements IWorkArrangeFixedMachineService {

    @Override
    public boolean submitWaFixedMachine(Long arrangeId, List<WorkArrangeFixedMachineSubmitDTO> data, String arrangeType) {
        Set<Long> exclude = new HashSet<>();
        if (Func.isNotEmpty(data)) {
            exclude = data.stream().map(WorkArrangeFixedMachineSubmitDTO::getId).filter(Objects::nonNull).collect(Collectors.toSet());
        }
        boolean removeOldSuccess = removeByArrangeId(arrangeId, exclude);
        if (!Func.isNull(data) && !Func.isEmpty(data)) {
            List<WorkArrangeFixedMachine> fixedMachines = new ArrayList<>();
            for (WorkArrangeFixedMachineSubmitDTO item : data) {
                WorkArrangeFixedMachine newMachine = new WorkArrangeFixedMachine();
                newMachine.setArrangeId(arrangeId);
                newMachine.setFixedMachineId(item.getFixedMachineId());
                newMachine.setArrangeType(arrangeType);
                if (item.getId() != null) {
                    newMachine.setId(item.getId());
                }
                fixedMachines.add(newMachine);
            }
            return removeOldSuccess && super.saveOrUpdateBatch(fixedMachines);
        } else {
            return removeOldSuccess;
        }
    }

    private boolean removeByArrangeId(Long arrangeId, Set<Long> exclude) {
        QueryWrapper<WorkArrangeFixedMachine> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(WorkArrangeFixedMachine::getId)
                .eq(WorkArrangeFixedMachine::getArrangeId,arrangeId)
                .notIn(Func.isNotEmpty(exclude), WorkArrangeFixedMachine::getId, exclude)
                .eq(WorkArrangeFixedMachine::getIsDeleted,0);
        List<WorkArrangeFixedMachine> entities = baseMapper.selectList(queryWrapper);
        if (!Func.isEmpty(entities)) {
            List<Long> ids = entities.stream().map(WorkArrangeFixedMachine::getId).collect(Collectors.toList());
            return super.deleteLogic(ids);
        }
        return true;
    }

    @Override
    public List<WorkArrangeFixedMachineVO> infoByArrangeId(Long arrangeId) {
        if (Func.isNull(arrangeId)) {
            throw new RuntimeException("没有配工ID");
        }
        List<WorkArrangeFixedMachineVO> res = baseMapper.selectInfoByArrangeId(arrangeId);
        for (WorkArrangeFixedMachineVO re : res) {
            re.setFixedMachinePositionName(DictBizCache.getValue("machine_position", re.getFixedMachinePosition()));
        }
        return res;
    }

    @Override
    public boolean removeByArrangeId(Long arrangeId) {
        QueryWrapper<WorkArrangeFixedMachine> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(WorkArrangeFixedMachine::getId)
                .eq(WorkArrangeFixedMachine::getArrangeId,arrangeId)
                .eq(WorkArrangeFixedMachine::getIsDeleted,0);
        List<WorkArrangeFixedMachine> entities = baseMapper.selectList(queryWrapper);
        if (!Func.isEmpty(entities)) {
            List<Long> ids = entities.stream().map(WorkArrangeFixedMachine::getId).collect(Collectors.toList());
            return super.deleteLogic(ids);
        }
        return true;
    }

    @Override
    public Integer fixedMachineKindsByarrangeIds(List<Long> arrangeIds) {
        if (!Func.isEmpty(arrangeIds)) {
            return 0;
        }
        QueryWrapper<WorkArrangeFixedMachine> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(WorkArrangeFixedMachine::getFixedMachineId)
                .eq(WorkArrangeFixedMachine::getIsDeleted, 0)
                .in(WorkArrangeFixedMachine::getArrangeId, arrangeIds);
        List<WorkArrangeFixedMachine> data = baseMapper.selectList(queryWrapper);
        if (!Func.isEmpty(data)) {
            List<Long> fmIds = data.stream().map(WorkArrangeFixedMachine::getFixedMachineId).distinct().collect(Collectors.toList());
            return fmIds.size();
        } else {
            return 0;
        }

    }
}
